<?php

class Jd_users_model extends MY_Model
{


    const USER = 'users';
    public $name;
    public $password;
    public $statu;
    public $group;
    public $gold;
    public $money;
    public $recommender;
    public $phone;
    public $channel;
    public $commission;
    public $team;


    public function __construct()
    {
        $this->modelename = 'Jd_users_model';
        $this->tablename = 'users';
        parent::__construct();
    }


    public function adduser($row)
    {
        return $this->add($this->tablename, $row);
    }


    /**
     *  通过用户ID获取用户信息
     */
    public function GetUserById($uid = 0, $select = "*")
    {
        $result = $this->db->select($select)->from('users')->where("id=" . $uid)->get()->result_array();
        return $result[0];
    }


    public function GetUserByName($username, $select = "*")
    {
        $webid = $this->web_id;
        $result = $this->db->select($select)->from('users')->where("username='$username' AND webid=$webid")->get()->result_array();
        if (empty($result)) {
            return false;
        }

        return $result[0];
    }

    public function GetUserByPhone($phone, $select = "*")
    {
        $webid = $this->web_id;
        $result = $this->db->select($select)->from('users')->where("phone='$phone' AND webid=$webid")->get()->result_array();

        if (empty($result)) {
            return false;
        }
        return $result[0];
    }


    /**
     *
     * 更新用户金币与金钱数据
     * @param $array
     * @return type
     */
    public function updata_user_money($sql, $user_id)
    {
        $sql = $sql . ' ' . "where id=" . $user_id;
        return $this->db->query($sql);
    }


    public function updata_user($row)
    {
        $user_id = $row['user_id'];
        unset($row['user_id']);
        return $this->update($this->tablename, "id", $user_id, $row);
    }


    /**
     *
     * 根据用户组获取用户列表
     * @param $group
     * @param string $select
     * @return mixed
     */
    public function getuserbygroup($group, $select = "*")
    {
        return $result = $this->db->select($select)->from('users')->where("group=" . $group . " AND webid=" . $this->web_id)->get()->result_array();
    }


    public function getdatebydate($web_id = "")
    {

        if (!$web_id) {
            $web_id = $this->web_id;
        }
        $sql = "select  sum( money) AS 'money',sum( gold) AS 'gold'
from jd_users  
 where `group`=2  AND  iskefu=0  AND webid=$web_id";
        return $result = $this->GetBySql($sql, 0);
    }


    public function getreg($date1 = '', $date2 = "", $web_id = "")
    {

        if ($web_id == '') {
            $web_id = $this->web_id;
        }
        if (!$date1) {
            $date1 = date('Y-m-d', time() - 24 * 3600);
            $date1 = strtotime($date1);
        } else {
            $date1 = strtotime($date1);
        }
        if (!$date2) {
            $date2 = date('Y-m-d');
            $date2 = strtotime($date2);
        } else {
            $date2 = strtotime($date2);
        }
        if ($web_id != 0) {
            $sql = "select count(1) as 'regnum'
from jd_users  
 where  regtime> $date1 AND regtime<$date2 AND webid=$web_id";
        } else {
            $sql = "select count(1) as 'regnum'
from jd_users  
 where  regtime> $date1 AND regtime<$date2";
        }
        return $result = $this->GetBySql($sql, 0);
    }


    /**
     * 获取用户所有邀请的且进行投资过的用户
     */
    public function tuijiansend($recommender_id)
    {
        $sql = "select count(1) as 'num'
from jd_users  as  p1
left join jd_user_total as p2 on p1.id=p2.user_id
 where p1.recommender_id=$recommender_id  AND p2.task_nub>0";
        $result = $this->GetBySql($sql, 0);
        $sql = "select count(1) as 'num'
from jd_users  as  p1
left join jd_user_total as p2 on p1.id=p2.user_id
 where p1.recommender_id=$recommender_id";
        $result2 = $this->GetBySql($sql, 0);
        if ($result >= 20 && $result['num'] / $result2['num'] >= 0.6) {
            return true;
        }
        return false;
    }

    public function addvip($user_id)
    {
        $sql = "update jd_users set VIP=1 where id= $user_id";
        $this->db->query($sql);
    }


    public function getUserShop($user_id)
    {
        $sql = "select name from  jd_shop where  user_id=$user_id AND status=1";
        return $this->GetBySql($sql, 1);
    }

    public function checkshop($name = "")
    {
        $sql = "select 1 from  jd_shop where  name='$name' AND status!=2 ";
        return $this->GetBySql($sql, 0);
    }

    public function getshopnub($user_id)
    {
        $sql = "select count(1) as `num` from  jd_shop where  user_id=$user_id AND status=3";
        return $this->GetBySql($sql, 0);
    }

    public function addshop($row)
    {


        $res = $this->checkshop($row['name']);
        if ($res) {
            return false;
        }

        return $this->add('shop', $row);
    }

    public function getshopbydid($id)
    {
        $sql = "select `status`,user_id from  jd_shop where  id=$id ";
        return $this->GetBySql($sql, 0);
    }

    public function shopRefuse($id, $remark, $status = 2)
    {
        $result = $this->getshopbydid($id);

        if (($result['status'] != 0 && $status == 2) || ($result['status'] != 1 && $status == 3)) {
            return false;
        }


        $r = $this->update('shop', "id", $id, array('status' => $status, 'remark' => $remark));
        if($status==3){
            $r = $this->update('shop', "id", $id, array('update' => time()));
        }

        return $r;
    }

    public function approveshop($id)
    {
        $result = $this->getshopbydid($id);
        if ($result['status'] != 0) {
            return false;
        }
        $r = $this->update('shop', "id", $id, array('status' => 1));
        if ($r) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 获取客服
     * @param $user_id
     */
    public function getneedset($web_id)
    {
        $sql = "select username,commission 
from jd_users  
 where `group`=5   AND webid=$web_id  order by id asc";
        return $this->GetBySql($sql, 1);
    }

    public function dbsetcustomer($web_id, $row)
    {
        if(!$web_id||empty($row)){
            return ;
        }
        $num = count($row);
        $sql = "update  jd_users set kefu=(id%$num+1)  where `kefu`=0  AND webid=$web_id";
        return $this->db->query($sql);
    }



}
